package pl.agh.convexhulltutor.structures;

import java.awt.Color;

import pl.agh.convexhulltutor.algorithms.Algorithm;

public class Line {
	public static final Color DEFAULT_COLOR = Color.YELLOW;
	public static final Color CONVEX_HULL_COLOR = Color.GREEN;
	public static final Color UPPER_CONVEX_HULL_COLOR = Color.CYAN;
	public static final Color LOWER_CONVEX_HULL_COLOR = Color.MAGENTA;

	private Point point1;
	private Point point2;
	private Color color;

	public Line(Point point1, Point point2) {
		this(point1, point2, DEFAULT_COLOR);
	}

	public Line(Point point1, Point point2, Color color) {
		this.point1 = point1;
		this.point2 = point2;
		this.color = color;
	}

	public Line(Line line) {
		point1 = new Point(line.getPoint1());
		point2 = new Point(line.getPoint2());
		color = line.color;
	}

	public Point getPoint1() {
		return point1;
	}

	public Point getPoint2() {
		return point2;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public double getX1() {
		return point1.getX();
	}

	public double getY1() {
		return point1.getY();
	}

	public double getX2() {
		return point2.getX();
	}

	public double getY2() {
		return point2.getY();
	}

	public boolean isIdentical(Line l) {
		return point1.isIdentical(l.point1) && point2.isIdentical(l.point2)
				&& color == l.color;
	}

	public int getDeterminantSign(Point point) {
		return Algorithm.getDeterminantSign(point1, point2, point);
	}

	public double getDistanceToPoint(Point point) {
		double a = (point1.getY() - point2.getY())
				/ (point1.getX() - point2.getX());
		double b = point1.getY() - a * point1.getX();
		double A = a;
		double B = -1.0;
		double C = b;
		return Math.abs(A * point.getX() + B * point.getY() + C)
				/ Math.sqrt(A * A + B * B);
	}

	@Override
	public String toString() {
		return "<" + point1 + ", " + point2 + ", " + color + ">";
	}
}
